﻿using System;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;
using TSB.FarmPowerShellWizard.ControlTemplates.TSB.FarmPowerShellWizard;
using TSB.FarmPowerShellWizard.Entities;
using TSB.FarmPowerShellWizard.Repository;

namespace TSB.FarmPowerShellWizard.Layouts.TSB.FarmPowerShellWizard
{
    public partial class PSW_ServiceApps : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            rptServiceApps.ItemCommand += rptServiceApps_ItemCommand;
            if (!IsPostBack)
            {
                var serviceAppRepository = new ServiceAppRepository();
                var serviceApps = serviceAppRepository.GetServiceApps();

                rptServiceApps.DataSource = serviceApps;
                rptServiceApps.ItemDataBound += rptServiceApps_ItemDataBound;
               
                rptServiceApps.DataBind();
            }
        }

        
        void rptServiceApps_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
            {
                return;
            }

            var ltName = e.Item.FindControl("ltName") as Literal;
            var ltTypeName = e.Item.FindControl("ltTypeName") as Literal;
            var btnGo = e.Item.FindControl("btnGo") as Button;
            var lnk = e.Item.FindControl("lnk") as HyperLink;
            var serviceApp = e.Item.DataItem as ServiceApplication;

            if (ltName == null || ltTypeName == null || btnGo == null || lnk  == null || serviceApp == null)
            {
                return;
            }


            ltName.Text = serviceApp.Name;
            ltTypeName.Text = serviceApp.Type;

            if (!serviceApp.EnabledScripting)
            {
                lnk.Visible = false;
                btnGo.Visible = false;
                return;
            }

            if (serviceApp.Type == "Search Service Application")
            {
                btnGo.Visible = false;
                lnk.Visible = true;
                lnk.Text = "Go";
                lnk.NavigateUrl = "PSW-SearchApplication.aspx?searchApp=" + serviceApp.Name;
                return;
            }

            lnk.Visible = false;
            btnGo.CommandArgument = serviceApp.Name;
            btnGo.CommandName = "G";
        }

        void btnGo_Click(object sender, EventArgs e)
        {
            var serviceAppName = ((Button) sender).CommandArgument;

            SetPowerShellText(serviceAppName);
        }

        void rptServiceApps_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            var serviceAppName = e.CommandArgument != null ? e.CommandArgument.ToString() : string.Empty;

            var serviceAppRepository = new ServiceAppRepository();
            SetPowerShellText(serviceAppRepository.GetServiceAppScript(serviceAppName));
        }


        void SetPowerShellText(string script)
        {
            if (string.IsNullOrEmpty(script))
            {
                phScriptContainer.Visible = false;
                txtPowerShell.Text = string.Empty;
                return;
            }

            phScriptContainer.Visible = true;
            txtPowerShell.Text = script;
        }
    }
}
